﻿using Core.Ndatax.Domain.Ris;
using Core.Ndatax.Domain.Ris.CheckInOrRegister;
using Core.Ndatax.Infrastructure.Entitys;
using Xman.Extensions.Core;

namespace Core.Ndatax.Infrastructure.Common
{
    public class MapperHelper
    {
        public static Domain.Ris.CancelRegisterOrCheckInDto.CancelRegisterOrCheckInDto CreateCancelRegisterDto(List<Domain.Ris.GetCancelRegisterDto> input, List<string> sheetIdList)
        {
            var result = new Domain.Ris.CancelRegisterOrCheckInDto.CancelRegisterOrCheckInDto();
            var requestDto = input.First();

            #region 患者信息映射

            result.PatientInfo.patientID = requestDto.PatientID;
            result.PatientInfo.name = requestDto.PatientName;
            result.PatientInfo.sex = requestDto.PatientSex.To<int>();
            result.PatientInfo.datetimeOfBirth = requestDto.PatientBirth.PadRight(14, '0');
            result.PatientInfo.age = requestDto.PatientAge;
            result.PatientInfo.idNumber = requestDto.PatientIdentityNo;
            result.PatientInfo.placeOfBirth = requestDto.PatientHomeAddress;
            result.PatientInfo.address = requestDto.PatientHomeAddress;
            result.PatientInfo.telephone = requestDto.PatientHomeTel;
            result.PatientInfo.patientType = requestDto.PatientStyle.To<int>();
            result.PatientInfo.registrationType = 0;
            result.PatientInfo.registrationDepID = requestDto.BillingDeptCode;
            result.PatientInfo.registrationDepName = requestDto.BillingDeptName;
            result.PatientInfo.outpatientID = requestDto.OutPatientID;
            result.PatientInfo.hospitalizationID = requestDto.InPatientID;
            result.PatientInfo.healthExaminationID = requestDto.MedicalCardNo;
            result.PatientInfo.outpatientID = requestDto.OutPatientID;
            result.PatientInfo.bedNumber = requestDto.PatientBedNo;
            result.PatientInfo.visitNo = 0;

            #endregion

            var list = new List<Domain.Ris.CancelRegisterOrCheckInDto.Requestinfolist>();
            sheetIdList.ForEach(t =>
            {
                var info = new Domain.Ris.CancelRegisterOrCheckInDto.Requestinfolist();
                info.RequestInfo.RISRequisitionID = requestDto.RISRequisitionID;
                info.RequestInfo.OperatorId = requestDto.OperateDoctorID;
                info.RequestInfo.OperatorName = requestDto.OperateDoctor;
                info.RequestInfo.OperateDT = requestDto.OperateDoctor;
                list.Add(info);
            });
            result.RequestInfoList = list.ToArray();
            return result;
        }

        public static Domain.Ris.CheckInOrRegister.CheckInOrRegisterDto CreateCheckInOrRegisterDto(List<GetCheckInOrRegisterDto> input, EnumRisOperateFlag flag)
        {
            var requestDto = input.First();
            var checkIn = new Domain.Ris.CheckInOrRegister.CheckInOrRegisterDto();

            #region 患者信息映射

            checkIn.PatientInfo.patientID = requestDto.RequestInfo.PatientID;
            checkIn.PatientInfo.name = requestDto.RequestInfo.PatientName;
            checkIn.PatientInfo.sex = requestDto.RequestInfo.PatientSex.To<int>();
            checkIn.PatientInfo.datetimeOfBirth = requestDto.RequestInfo.PatientBirth.PadRight(14, '0');
            checkIn.PatientInfo.idNumber = requestDto.RequestInfo.PatientIdentityNo;
            checkIn.PatientInfo.placeOfBirth = requestDto.RequestInfo.PatientHomeAddress;
            checkIn.PatientInfo.address = requestDto.RequestInfo.PatientHomeAddress;
            checkIn.PatientInfo.telephone = requestDto.RequestInfo.PatientHomeTel;
            checkIn.PatientInfo.patientType = requestDto.RequestInfo.PatientStyle.To<int>();
            checkIn.PatientInfo.registrationType = 0;
            checkIn.PatientInfo.registrationDepID = requestDto.RequestInfo.BillingDeptCode;
            checkIn.PatientInfo.registrationDepName = requestDto.RequestInfo.BillingDeptName;
            checkIn.PatientInfo.outpatientID = requestDto.RequestInfo.OutPatientID;
            checkIn.PatientInfo.hospitalizationID = requestDto.RequestInfo.InPatientID;
            checkIn.PatientInfo.healthExaminationID = requestDto.RequestInfo.MedicalCardNo;
            checkIn.PatientInfo.outpatientID = requestDto.RequestInfo.OutPatientID;
            checkIn.PatientInfo.inpatientAreaID = requestDto.RequestInfo.PatientWardNo;
            checkIn.PatientInfo.inpatientAreaName = requestDto.RequestInfo.PatientDeptName;
            checkIn.PatientInfo.bedNumber = requestDto.RequestInfo.PatientBedNo;
            checkIn.PatientInfo.visitNo = requestDto.RequestInfo.PatientAdmitTimes.To<int>();
            #endregion

            var reqList = new List<Domain.Ris.CheckInOrRegister.Requestinfolist>();
            foreach (var dtoItem in input)
            {

                var req = new Domain.Ris.CheckInOrRegister.Requestinfolist();

                #region 申请单信息

                req.RequestInfo = new Domain.Ris.CheckInOrRegister.Requestinfo();
                req.RequestInfo.sheetID = dtoItem.RequestInfo.SheetID;
                req.RequestInfo.inOrOutNumber = dtoItem.RequestInfo.PatientJZLSH;
                req.RequestInfo.checkType = dtoItem.RequestInfo.CheckType;
                req.RequestInfo.requestState = dtoItem.RequestInfo.RequestState.To<int>();
                req.RequestInfo.feeState = dtoItem.RequestInfo.FeeState.To<int>();
                req.RequestInfo.defaultModality = dtoItem.RequestInfo.ExamModality;
                req.RequestInfo.reqDate = dtoItem.RequestInfo.ReqSheetDate;
                req.RequestInfo.reqTime = dtoItem.RequestInfo.ReqSheetTime;
                req.RequestInfo.reqHospitalName = dtoItem.RequestInfo.HospitalName;
                req.RequestInfo.reqDepartmentID = dtoItem.RequestInfo.BillingDeptCode;
                req.RequestInfo.reqDepartmentName = dtoItem.RequestInfo.BillingDeptName;
                req.RequestInfo.reqDoctorID = dtoItem.RequestInfo.ReqSheetDoctorID;
                req.RequestInfo.reqDoctorName = dtoItem.RequestInfo.ReqSheetDoctor;
                req.RequestInfo.execDepartmentID = dtoItem.RequestInfo.ExamDeptCode;
                req.RequestInfo.execDepartmentName = dtoItem.RequestInfo.ExamDeptName;
                req.RequestInfo.urgent = dtoItem.RequestInfo.IsEmergent.To<int>();
                req.RequestInfo.medicalRecord = dtoItem.RequestInfo.HistorySummary;
                req.RequestInfo.clinicalDiagnosis = dtoItem.RequestInfo.ClinicalDiagnosis;
                req.RequestInfo.examPurpose = dtoItem.RequestInfo.ExamPurpose;
                req.RequestInfo.remark = dtoItem.RequestInfo.Remark;
                req.RequestInfo.sumFee = dtoItem.RequestInfo.TotalFee;
                req.RequestInfo.inPriorityLevel = dtoItem.RequestInfo.InPriorityLevel;
                req.RequestInfo.patientType = dtoItem.RequestInfo.PatientStyle.ToString();

                #endregion

                #region 检查项目

                var examList = new List<Examitemlist>();
                foreach (var examItem in dtoItem.RequestExamList)
                {
                    var exam = new Examitemlist();
                    exam.ExamItem = new Examitem();
                    exam.ExamItem.itemID = examItem.ExamID;
                    exam.ExamItem.medicalOrderNo = examItem.OrderID;
                    exam.ExamItem.itemCode = examItem.ItemCode;
                    exam.ExamItem.itemName = examItem.ExamProject;
                    exam.ExamItem.examBodyPart = examItem.ExamBodyPart;
                    exam.ExamItem.examMethod = examItem.ExamMethod;
                    exam.ExamItem.isHaveExamFilm = 0;
                    exam.ExamItem.examFloorID = 1;
                    exam.ExamItem.examFloorName = dtoItem.RequestInfo.ExamPlace;
                    exam.ExamItem.examQueueCode = dtoItem.RequestInfo.PhyQueueCode;
                    exam.ExamItem.examQueueName = dtoItem.RequestInfo.PhyQueueName;

                    #region 费用明细

                    var feeItemList = dtoItem.RequestFeeList.Where(t => t.SheetID == req.RequestInfo.sheetID && t.ExamID == examItem.ExamID).ToList();
                    var feeList = new List<Feelist>();
                    foreach (var feeItem in feeItemList)
                    {
                        var fee = new Feelist();
                        fee.Fee = new Fee();
                        fee.Fee.feeID = feeItem.ID;
                        fee.Fee.chargeID = feeItem.FEEID;
                        fee.Fee.chargeItemCode = feeItem.ExamID;
                        fee.Fee.chargeItemName = feeItem.FeeName;
                        fee.Fee.chargeItemUnit = feeItem.FeeUnit;
                        fee.Fee.chargeItemPrice = feeItem.FeePrice.ToString();
                        fee.Fee.chargeItemNumber = feeItem.FeeNum.To<int>();
                        fee.Fee.chargeItemType = feeItem.FeeStyle.To<int>();
                        feeList.Add(fee);
                    }

                    #endregion

                    exam.ExamItem.FeeList = feeList.ToArray();
                    examList.Add(exam);
                }

                req.RequestInfo.ExamItemList = examList.ToArray();

                #endregion

                #region 预约信息

                req.RequestInfo.AppointmentInfo = new Appointmentinfo();
                req.RequestInfo.AppointmentInfo.appointmentID = dtoItem.RequestInfo.ScheduleID;
                req.RequestInfo.AppointmentInfo.appointmentDate = dtoItem.RequestInfo.ReservationDate;
                req.RequestInfo.AppointmentInfo.appointmentTime = dtoItem.RequestInfo.ReservationTime;

                if (flag == EnumRisOperateFlag.AutoCheckIn || flag == EnumRisOperateFlag.CheckIn)
                {
                    req.RequestInfo.AppointmentInfo.appointmentState = 3;
                }
                else
                {
                    req.RequestInfo.AppointmentInfo.appointmentState = 1;
                }
                req.RequestInfo.AppointmentInfo.resourceID = dtoItem.RequestInfo.ResSourceID;
                req.RequestInfo.AppointmentInfo.appointmentAddr = dtoItem.RequestInfo.ExamPlace;
                req.RequestInfo.AppointmentInfo.appointmentType = dtoItem.RequestInfo.ExamModality;
                #endregion

                reqList.Add(req);
            }

            checkIn.RequestInfoList = reqList.ToArray();
            return checkIn;
        }

        public static Domain.Ris.UpdateRegisterDto.UpdateRegisterDto CreateUpdateRegisterDto(List<GetCheckInOrRegisterDto> input)
        {
            var result = new Domain.Ris.UpdateRegisterDto.UpdateRegisterDto();

            var reqList = new List<Domain.Ris.UpdateRegisterDto.Sheetidlist>();
            foreach (var dtoItem in input)
            {

                var req = new Domain.Ris.UpdateRegisterDto.Sheetidlist();

                #region 预约信息
                req.sheetID = dtoItem.RequestInfo.SheetID;
                req.appointmentID = dtoItem.RequestInfo.ScheduleID;
                req.appointmentDate = dtoItem.RequestInfo.ReservationDate;
                req.appointmentTime = dtoItem.RequestInfo.ReservationTime;
                req.appointmentState = 1;
                req.resourceID = dtoItem.RequestInfo.ResSourceID;
                req.appointmentAddr = dtoItem.RequestInfo.ExamPlace;
                req.appointmentType = dtoItem.RequestInfo.ExamModality;
                #endregion

                reqList.Add(req);
            }

            result.sheetIDList = reqList.ToArray();
            return result;
        }

    }
}
